package com.ews.onlineexchange.controller.pub;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.QualificationClass;
import com.ews.onlineexchange.model.QualificationLevel;
import com.ews.onlineexchange.model.Sector;
import com.ews.onlineexchange.service.QualificationClassService;
import com.ews.onlineexchange.service.SectorService;

@ViewScoped
@Named
public class QualificationConditionBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String qualificationClassId;
	private String sectorId;
	private Integer certLevel;
	private String childSectorSno;
	private Set<Sector> sectors = new LinkedHashSet<>();
	private List<Sector> childSectors = new ArrayList<>();
	private List<QualificationLevel> qualificationLevels = new ArrayList<>();
	private String agentServiceId;
	private boolean changed = true;

	public String getQualificationClassId() {
		return qualificationClassId;
	}

	public void setQualificationClassId(String qualificationClassId) {
		this.qualificationClassId = qualificationClassId;
		this.setChanged(true);
	}

	public String getSectorId() {
		return sectorId;
	}

	public void setSectorId(String sectorId) {
		this.sectorId = sectorId;
	}
	
	
	
	public String getChildSectorSno() {
		return childSectorSno;
	}

	public void setChildSectorSno(String childSectorSno) {
		this.childSectorSno = childSectorSno;
	}

	public List<Sector> getChildSectors() {
		if (isChanged())
			init();
		return childSectors;
	}

	public void setChildSectors(List<Sector> childSectors) {
		this.childSectors = childSectors;
	}

	public Integer getCertLevel() {
		return certLevel;
	}

	public void setCertLevel(Integer certLevel) {
		this.certLevel = certLevel;
	}
	
	public boolean isChanged() {
		return changed;
	}

	public void setChanged(boolean changed) {
		this.changed = changed;
	}
	
	public void init() {
		sectors = querySectors();
		if(!StringUtils.isBlank(getSectorId())) {
			childSectors = queryChildSectors();
		}
		qualificationLevels = queryQualificationLevels();
		this.setChanged(false);
	}

	public Set<Sector> getSectors() {
		if (isChanged())
			init();
		return sectors;
	}

	public void setSectors(Set<Sector> sectors) {
		this.sectors = sectors;
	}

	public List<QualificationLevel> getQualificationLevels() {		
		if (isChanged())
			init();
		return qualificationLevels;
	}

	public void setQualificationLevels(List<QualificationLevel> qualificationLevels) {
		this.qualificationLevels = qualificationLevels;
	}
	
	@Inject
	SectorService sectorService;
	
	public Set<Sector> querySectors() {
		return sectorService.querySectors(getQualificationClassId());		
	}
	
	public List<Sector> queryChildSectors() {
		return sectorService.getChildSectors(getSectorId());		
	}
	
	@Inject
	QualificationClassService qualificationClassService;
	
	public List<QualificationLevel> queryQualificationLevels() {
		List<QualificationLevel> resultList = new ArrayList<>();
		if (!StringUtils.isBlank(getQualificationClassId())) {
			QualificationClass qc = qualificationClassService.find(getQualificationClassId());
			
			resultList.addAll(qc.getSortedLevelOptions());
		}
		return resultList;
	}
	
	public String findQualificationClassId(String name) {
		QualificationClass qClass = qualificationClassService.findQualificationClass(name);
		if (qClass != null) {
			String id = qClass.getId();
			return id;
		}
		return null;
	}
	
	public void initChildSectors() {
		childSectors = queryChildSectors();
	}

	public String getAgentServiceId() {
		return agentServiceId;
	}

	public void setAgentServiceId(String agentServiceId) {
		this.agentServiceId = agentServiceId;
	}
	
	
}
